import { listDept } from "@/api/sys/dept";
import { deptTreeSelect } from "@/api/sys/user";

const useDeptStore = defineStore("dept", () => {
  const deptList = ref([]); // 部门列表
  const deptMap = reactive(new Map()); // 部门Map

  const deptTree = ref([]); // 部门树结构

  // 初始化部门Map
  listDept().then((res) => {
    deptList.value = res.data;
    res.data.map((item) => {
      deptMap.set(item.deptId, item);
    });
  });

  // 初始化部门树结构
  deptTreeSelect().then((res) => {
    deptTree.value = res.data;
  });

  // 获取部门
  function getDept(_key) {
    return deptMap.get(_key);
  }
  return { deptMap, deptTree, getDept };
});

export default useDeptStore;
